Ext.onReady(function() {
	Ext.QuickTips.init(); // 如要实现错误提示，需要此初始化
	Ext.form.Field.prototype.msgTarget = 'side';

	var height = this.document.documentElement.clientHeight;
	var width = this.document.documentElement.clientWidth;

	// top区域开始

	// top区域结束

	// 地点区域开始
	var areasm = new Ext.grid.CheckboxSelectionModel();

	var areacm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), areasm,
			{
				header : "地点编号",
				dataIndex : "areaID",
				sortable : true
			}, {
				header : "区域",
				dataIndex : "happenArea",
				sortable : true
			}, {
				header : "街道",
				dataIndex : "street",
				sortable : true
			}, {
				header : "社区",
				dataIndex : "communit",
				sortable : true
			}
	]);
    //            , {
    //                header : "单元网格",
    //                dataIndex : "cell",
    //                sortable : true
    //            }, {
    //                header : "责任网格",
    //                dataIndex : "dutyReg",
    //                sortable : true
    //            }
	var aaProxy = new Ext.data.HttpProxy({
				url : "area",
				method : "GET"
			});

	var areastore = new Ext.data.Store({
				proxy : aaProxy,
				reader : new Ext.data.JsonReader({})
			});
	areastore.load();
	var areapp = new Ext.grid.GridPanel({
		id : 'areapp',
		title : '地点信息',
		region : 'center',
		autoShow : true,
		height : height,
		width : width,
		split : true,
		minSize : 175,
		maxSize : 400,
		frame : true,
		columnLines : true,
		sm : areasm,
		store : areastore,
		cm : areacm,
		tbar : [{
			pressed : false,
			text : '添加地点',
			icon : "static/images/add.gif",
			handler : function() {
				var TareaID = new Ext.form.Field({
							id : 'areaID',
							name : 'areaID',
							allowBlank : true,
							fieldLabel : "地点编号"
						});
				var ThappenArea = new Ext.form.Field({
							id : 'happenArea',
							name : 'happenArea',
							allowBlank : true,
							fieldLabel : "区域"
						});
				var Tstreet = new Ext.form.Field({
							id : 'street',
							name : 'street',
							allowBlank : true,
							fieldLabel : "街道"
						});
				var Tcommunit = new Ext.form.Field({
							id : 'communit',
							name : 'communit',
							allowBlank : true,
							fieldLabel : "社区"
						});
				var formp = new Ext.form.FormPanel({
					title : "添加",
					closable : true,
					labelAlign : "right",
					buttonAlign : "center",
					// width : 350,
					// height : 500,
					autoHeight : true,
					autoWidth : true,
					frame : true,
					split : true,
					items : [TareaID, ThappenArea, Tstreet,Tcommunit],
					url : "area?method=add",
					method : "post",
					buttons : [{
						text : "确定",
						icon : "extjsLib/resources/static/images/default/dd/disk.png",
						handler : function() {
							formp.getForm().submit({
										success : function(form, action) {
											var msg = action.result.msg;
											if (msg == "yes") {
												areastore.reload();
												Ext.Msg.alert("成功",
														"恭喜，地点信息添加成功！");
												addwin.close();
											}
											if (msg == "no") {
												Ext.Msg.alert("失败",
														"地点号重复或不存在！");
											}
											// store.reload();
										},
										failure : function(response) {
											Ext.Msg
													.alert("失败",
															"对不起，地点信息添加异常！");
										}
									});
						}
					}, {
						text : "重置",
						icon : "extjsLib/resources/static/images/default/dd/delete.gif",
						handler : function() {
							formp.getForm().reset();
						}
					}]
				});
				var addwin = new Ext.Window({
							title : "",
							width : 350,
							// height : 350,
							closeAction : "close",
							closeable : true,
							items : formp,
							collapsible : true,
							autoHeight : true,
							modal : true
						});
				addwin.show();
			}
		}, {
			xtype : "tbseparator"
		}, {
			pressed : false,
			text : '地点删除',
			icon : "static/images/cross.gif",
			handler : function() {
				var selModel = areapp.getSelectionModel();
				if (selModel.hasSelection()) {
					Ext.Msg.confirm("确认", "您确定要删除选择的记录吗？", function(btn) {
						if (btn == "yes") {
							var records = selModel.getSelections();
							var ids = [];
							for (var i = 0; i < records.length; i++) {
								ids.push(records[i].get("areaID")); // 获取选择的条目id
							}
							Ext.Ajax.request({
										url : "area?method=delete",
										params : {
											ids : ids
										},
										method : "post",
										success : function(form, action) {
											Ext.MessageBox.alert("结果", "删除成功！");
											areastore.reload();
										},
										failure : function() {
											var messagestr = response.result.msg;
											alert(messagestr);
											areastore.reload();
											// Ext.Msg
											// .alert(
											// "结果",
											// "对不起，删除异常！");
										}
									});

						}
					});
				} else {
					Ext.Msg.alert("错误", "请选择要删除的行！");
				}
			}
		}, {
			xtype : "tbseparator"
		}, {
			pressed : false,
			text : '修改地点信息',
			icon : "static/images/details.gif",
			handler : function() {
				// Ext.Msg.alert("修改");
				var selModel = areapp.getSelectionModel();
				var record;
				if (!selModel.hasSelection()) {
					Ext.Msg.alert("错误", "请选择要修改的行！");
				} else if (selModel.getSelections().length > 1) {
					Ext.Msg.alert("错误", "一次只能选择一行！");
				} else {
					record = selModel.getSelected();

					var TareaID = new Ext.form.Field({
                            id : 'areaID',
                            name : 'areaID',
                            allowBlank : true,
                            fieldLabel : "地点编号"
                        });
                var ThappenArea = new Ext.form.Field({
                            id : 'happenArea',
                            name : 'happenArea',
                            allowBlank : true,
                            fieldLabel : "区域"
                        });
                var Tstreet = new Ext.form.Field({
                            id : 'street',
                            name : 'street',
                            allowBlank : true,
                            fieldLabel : "街道"
                        });
                var Tcommunit = new Ext.form.Field({
                            id : 'communit',
                            name : 'communit',
                            allowBlank : true,
                            fieldLabel : "社区"
                        });

					var form = new Ext.form.FormPanel({
						width : 350,
						labelAlign : "right",
						frame : true,
						items :[TareaID, ThappenArea, Tstreet,Tcommunit],
						url : "area?method=edit",
						buttonAlign : "center",
						method : "post",
						buttons : [{
							text : "地点修改",
							icon : "extjsLib/resources/static/images/default/dd/disk.png",
							handler : function() {
								var json = {
									success : function(form, action) {
										var msg = action.result.msg;
										if (msg == "yes") {
											Ext.Msg.alert("成功", "信息修改成功！");
											win.close();
											areastore.reload();
										} else {
											Ext.Msg.alert("失败", "信息修改异常！");
										}
									}
								};
								form.getForm().submit(json);
								areastore.reload();
							}
								// win.close();
						}, {
							text : "关闭",
							icon : "extjsLib/resources/static/images/default/dd/delete.gif",
							handler : function() {
								win.close();
							}
						}]

					});
					var win = new Ext.Window({
								title : "地点修改",
								id : "edit",
								// width : 500,
								width : 350,
								modal : true,
								autoHeight : true,
								items : [form]
							});
					win.show();

					// set the form default values
					form.getForm().setValues({
								"areaID" : record.get("areaID"),
								"happenArea" : record.get("happenArea"),
								"street" : record.get("street"),
                                "communit" : record.get("communit")
							});
				}
			}
		}, {
			xtype : "tbseparator"
		}, {
			pressed : false,
			text : '所有地点',
			icon : "static/images/table_refresh.png",
			handler : function() {
				areastore.proxy = aaProxy;
				areastore.load({
							params : {
								start : 0,
								limit : 11
							}
						});
			}
		}],
		bbar : [new Ext.PagingToolbar({
			store : areastore,
			displayInfo : true,
			pageSize : 11,
			beforePageText : "当前第",
			afterPageText : "页，共{0}页",
			lastText : "尾页",
			nextText : "下一页",
			prevText : "上一页",
			firstText : "首页",
			refreshText : "刷新页面",
			displayInfo : true,
			displayMsg : "当前显示 {0} - {1}条, 共 {2}",
			emptyMsg : "没有记录"
		})]
	});
	var win = new Ext.Panel({
				layout : 'border',
				height : height,
				width : width,
				items : [areapp]
			});
	win.render(Ext.getBody());
});